package com.aiju.cash.ui.enter.util;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.ImageView;

import com.aiju.app.SimpleHttpCallback;
import com.aiju.app.config.ConfigManager;
import com.aiju.bean.JsonData;
import com.aiju.cash.core.api.EnterApi;
import com.aiju.cash.core.api.EnterImageApi;
import com.aiju.cash.util.ImageDialogUtil;
import com.aiju.common.image.AiJuImageLoaderProxy;
import com.aiju.utils.L;
import com.aiju.utils.T;
import com.aiju.widgit.dialog.DialogHelper;
import com.yuyh.library.imgsel.ImgSelActivity;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.File;
import java.util.List;

import okhttp3.Call;

import static android.app.Activity.RESULT_OK;
import static com.aiju.cash.ui.good.GoodAddActivity.REQUEST_CODE;

/**
 * @author: 佐为
 * @date: 2017-06-07 09:59
 * @version: V1.0
 * @Description: 上传图片，加载图片，删除图片
 */
public class EnterHelper {

    /**
     * 显示图片
     *
     * @param intent
     * @param helper
     * @param imageView
     */
    public static void displayImage(Intent intent, DialogHelper helper, final ImageView imageView) {
        if (intent == null || imageView == null) {
            return;
        }
        String image_id = intent.getStringExtra("image_id");
        if (!TextUtils.isEmpty(image_id)) {
            new EnterApi(helper).getImageInfo(image_id, new SimpleHttpCallback() {
                @Override
                protected void onDataSuccess(String data) {
                    if (data.contains("分割线")) {
                        data = data.split("分割线")[1];
                        AiJuImageLoaderProxy.getInstance().displayImage(data, imageView, -1);
                    }
                }
            });
        }
    }

    /**
     * 上传图片
     */
    public static void uploadImageFile(final Activity activity, final DialogHelper helper, File file) {
        uploadImageFile(activity, helper, new Intent(), file);
    }

    /**
     * 上传图片
     */
    public static void uploadImageFile(final Activity activity, final DialogHelper helper, final Intent intent, File file) {
        helper.showLoadingProgressDialog();
        new EnterImageApi().uploadShopImage(ConfigManager.getInstance().getUser().getToken(), file, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                helper.dismissProgressDialog();
                T.ToastNetError();
            }

            @Override
            public void onResponse(String response, int id) {
                helper.dismissProgressDialog();
                JsonData jsonData = new JsonData(response);
                if (jsonData.isSuccess()) {
                    intent.putExtra("data", jsonData.getData().toString());
                    activity.setResult(200, intent);
                    activity.finish();
                    T.ToastShort("图片上传成功 ");
                } else {
                    T.ToastShort(jsonData.getMsg());
                }
            }
        });
    }

    /**
     * 解析选择图片
     */
    public static File onActivityResult(ImageView imageView, int requestCode, int resultCode, Intent data) {
        File imageFile = null;
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK && data != null) {
            List<String> pathList = data.getStringArrayListExtra(ImgSelActivity.INTENT_RESULT);
            for (String path : pathList) {
                imageFile = new File(path);
                AiJuImageLoaderProxy.getInstance().displayImage(path, imageView, -1);
                L.d("相册选取地址：" + path);
            }
        } else if (requestCode == ImageDialogUtil.REQUEST_CAMERA_1 && resultCode == RESULT_OK && data != null) {
            String path = ImageDialogUtil.saveCameraImage(data);
            imageFile = new File(path);
            AiJuImageLoaderProxy.getInstance().displayImage(path, imageView, -1);
            L.d("拍照地址：" + path);
        }
        return imageFile;
    }
    /**
     * 解析选择图片
     */
    public static File onActivityResultByRequestCode(ImageView imageView,  int resultCode, Intent data) {
        File imageFile = null;
        if (resultCode == RESULT_OK && data != null) {
            List<String> pathList = data.getStringArrayListExtra(ImgSelActivity.INTENT_RESULT);
            for (String path : pathList) {
                imageFile = new File(path);
                AiJuImageLoaderProxy.getInstance().displayImage(path, imageView, -1);
                L.d("相册选取地址：" + path);
            }
        } else if (resultCode == RESULT_OK && data != null) {
            String path = ImageDialogUtil.saveCameraImage(data);
            imageFile = new File(path);
            AiJuImageLoaderProxy.getInstance().displayImage(path, imageView, -1);
            L.d("拍照地址：" + path);
        }
        return imageFile;
    }
}
